/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "LOGR_gen_pages.h"
#include "LOGR_independent_pages.h"

WebGenPagePtr
new_web_gen_page_node(void)
{
  WebGenPagePtr gpn = (WebGenPagePtr)calloc(1, sizeof(WebGenPageStruct));
  if(!gpn) {
      fprintf(stderr,"Out of Memory new_web_gen_page_node\n");
      abort();
  }
  return gpn;
}

void
delete_web_gen_page_node(WebGenPagePtr gpn)
{	
    if(!gpn) return;

    free(gpn); gpn = NULL;
}

void
LOGR_set_type_gen_page(WebGenPagePtr gpn,TypePage tp)
{
  if(!gpn) return;
  gpn->tp = tp;
}

TypePage
LOGR_get_type_gen_page(WebGenPagePtr gpn)
{
  if(!gpn) return 0;
  return gpn->tp;
}

void
LOGR_set_info_gen_page(WebGenPagePtr gpn,void *np)
{
  if(!gpn) return;
  gpn->nodeptr = np;
}

void *
LOGR_get_info_gen_page(WebGenPagePtr gpn)
{
  if(!gpn) return NULL;
  return gpn->nodeptr;
}

void
LOGR_set_gen_naccesses(WebGenPagePtr gpn,long num)
{
  if(!gpn) return;
  gpn->naccesses = num;
}

void
LOGR_inc_gen_naccesses(WebGenPagePtr gpn)
{
  if(!gpn) return;
  gpn->naccesses++;
}

void
LOGR_add_gen_naccesses(WebGenPagePtr gpn,long hits)
{
  if(!gpn) return;
  gpn->naccesses += hits;
}

long
LOGR_get_gen_naccesses(WebGenPagePtr gpn)
{
  if(!gpn) return 0;
  return gpn->naccesses;
}

void
LOGR_set_gen_total_bytes(WebGenPagePtr gpn,long bytes)
{
  if(!gpn) return;
  gpn->tbytes = bytes;
}

void
LOGR_add_gen_total_bytes(WebGenPagePtr gpn,long bytes)
{
  if(!gpn) return;
  gpn->tbytes += bytes;
}

long
LOGR_get_gen_total_bytes(WebGenPagePtr gpn)
{
  if(!gpn) return 0;
  return gpn->tbytes;
}

void
LOGR_set_gen_nacc_static(WebGenPagePtr gpn,long num)
{
  if(!gpn) return;
  gpn->nacc_stat = num;
}

void
LOGR_inc_gen_nacc_static(WebGenPagePtr gpn)
{
  if(!gpn) return;
  gpn->nacc_stat++;
}

void
LOGR_add_gen_nacc_static(WebGenPagePtr gpn,long hits)
{
  if(!gpn) return;
  gpn->nacc_stat += hits;
}

long
LOGR_get_gen_nacc_static(WebGenPagePtr gpn)
{
  if(!gpn) return 0;
  return gpn->nacc_stat;
}


void
LOGR_set_gen_nacc_dynamic(WebGenPagePtr gpn,long num)
{
  if(!gpn) return;
  gpn->nacc_dyna = num;
}

void
LOGR_inc_gen_nacc_dynamic(WebGenPagePtr gpn)
{
  if(!gpn) return;
  gpn->nacc_dyna++;
}

void
LOGR_add_gen_nacc_dynamic(WebGenPagePtr gpn,long hits)
{
  if(!gpn) return;
  gpn->nacc_dyna += hits;
}

long
LOGR_get_gen_nacc_dynamic(WebGenPagePtr gpn)
{
  if(!gpn) return 0;
  return gpn->nacc_dyna;
}

int
LOGR_is_access_gen_lower(WebGenPagePtr u, WebGenPagePtr v)
{
  return (u->naccesses < v->naccesses);
}

int
LOGR_is_access_gen_bigger(WebGenPagePtr u,WebGenPagePtr v)
{
  return(u->naccesses > v->naccesses);
}

int
LOGR_is_access_stat_gen_lower(WebGenPagePtr u, WebGenPagePtr v)
{
  return (u->nacc_stat < v->nacc_stat);
}

int
LOGR_is_access_stat_gen_bigger(WebGenPagePtr u,WebGenPagePtr v)
{
  return(u->nacc_stat > v->nacc_stat);
}


int
LOGR_is_tot_access_gen_lower(WebGenPagePtr u, WebGenPagePtr v)
{
    long tu = u->naccesses + u->nacc_stat + u->nacc_dyna;
    long tv = v->naccesses + v->nacc_stat + v->nacc_dyna;
    return (tu < tv);
}

int
LOGR_is_tot_access_gen_bigger(WebGenPagePtr u,WebGenPagePtr v)
{
    long tu = u->naccesses + u->nacc_stat + u->nacc_dyna;
    long tv = v->naccesses + v->nacc_stat + v->nacc_dyna;
    return (tu > tv);
}

const char *
LOGR_get_gen_url(WebGenPagePtr gnp)
{
    if(!gnp)
	return NULL;

    if(gnp->tp == PWEBNODE)
    {
	PNODE v = (PNODE)gnp->nodeptr;
	return  v ? get_label_vertex(v) : NULL;
    }
    else if(gnp->tp == PWEBINDEPENDENT)
    {
	WebIndependentPagePtr opn = (WebIndependentPagePtr)gnp->nodeptr;
	return opn ? LOGR_get_ip_url(opn) : NULL;
    }
    return NULL;
}

long
LOGR_get_gen_sizebytes(WebGenPagePtr gnp)
{
    if(!gnp)
	return -1;
    if(gnp->tp == PWEBNODE)
    {
	PNODE v = (PNODE)gnp->nodeptr;
	char *size = v ? (char *)get_attribute(v,"size") : NULL; 
	long nsize = size ? atoi(size) : -1 ;
	return nsize;
	/*char * sweight = v ? get_weight_vertex(v) : NULL;
	long weight = sweight ? atoi(sweight) : -1 ;
	return  weight;*/
    }
    else if(gnp->tp == PWEBINDEPENDENT)
    {
	WebIndependentPagePtr opn = (WebIndependentPagePtr)gnp->nodeptr;
	return opn ? LOGR_get_ip_sizebytes(opn) : -1;
    }
    return -1;
}

int
LOGR_get_gen_num(WebGenPagePtr gnp)
{
    if(!gnp)
	return -1;

    if(gnp->tp == PWEBNODE)
    {
	PNODE v = (PNODE)gnp->nodeptr;
	return  v ? get_vertex_number(v) : -1;
    }
    else if(gnp->tp == PWEBINDEPENDENT)
    {
	WebIndependentPagePtr opn = (WebIndependentPagePtr)gnp->nodeptr;
	return opn ? LOGR_get_ip_num(opn) : -1;
    }
    return -1;
}

long
LOGR_get_gen_tot_naccesses(WebGenPagePtr gnp)
{
    if(!gnp)
	return -1;

    if(gnp->tp == PWEBNODE)
    {
	PNODE v = (PNODE)gnp->nodeptr;
	return  v ? (long)get_counter(v) : -1;
    }
    else if(gnp->tp == PWEBINDEPENDENT)
    {
	WebIndependentPagePtr opn = (WebIndependentPagePtr)gnp->nodeptr;
	return opn ? LOGR_get_ip_naccesses(opn) : -1;
    }
    return -1;
}

const char *
LOGR_get_gen_title(WebGenPagePtr gnp)
{
    if(!gnp)
	return NULL;

    if(gnp->tp == PWEBNODE)
    {
	PNODE v = (PNODE)gnp->nodeptr;
	return  v ? get_attribute(v,"title"): NULL;
    }
    else if(gnp->tp == PWEBINDEPENDENT)
    {
	return NULL;
    }
    return NULL;
}


const char *
LOGR_get_gen_mime(WebGenPagePtr gnp)
{
    if(!gnp)
	return NULL;

    if(gnp->tp == PWEBNODE)
    {
	PNODE v = (PNODE)gnp->nodeptr;
	return  v ? get_attribute(v,"mime"): NULL;
    }
    else if(gnp->tp == PWEBINDEPENDENT)
    {
	WebIndependentPagePtr opn = (WebIndependentPagePtr)gnp->nodeptr;
	return opn ? LOGR_get_ip_mime(opn) : NULL;
    }
    return NULL;
}

short
LOGR_is_gen_page_independent(WebGenPagePtr spn)
{
    if(!spn)	
	return 0;
    return (spn->tp == PWEBINDEPENDENT);
}
